package org.x.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.x.common.web.exception.BusinessException;
import org.x.user.mapper.SysPermMapper;
import org.x.user.mapper.SysRoleMapper;
import org.x.user.mapper.SysRolePermMapper;
import java.lang.Boolean;
import java.lang.Override;
import java.lang.String;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.x.user.entity.SysPerm;
import org.x.user.entity.SysRole;
import org.x.user.entity.SysRolePerm;
import org.x.user.dto.PermDTO;
import org.x.user.dto.RoleDTO;
import org.x.user.dto.RolePermDTO;
import org.x.user.query.PermQuery;
import org.x.user.service.PermService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.Date;

import static org.x.common.base.result.ResultCode.*;

/**
 * 权限管理的实现
 *
 * @author 夏包包
 * @date 2025-03-08 13:39:03
 */
@Service
public class PermServiceImpl extends ServiceImpl<SysPermMapper, SysPerm> implements PermService {

    @Autowired
    private SysPermMapper permMapper;

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private SysRolePermMapper rolePermMapper;

    @Override
    public Boolean addPerm(PermDTO permDTO) {
        QueryWrapper<SysPerm> queryWrapper = Wrappers.<SysPerm>query().eq("perm_name", permDTO.getPermName());
        if (permMapper.selectOne(queryWrapper) != null) {
            //资源名称已存在
            throw new BusinessException(RESOURCE_NAME_EXIST);
        }
        SysPerm permDO = new SysPerm();
        permDO.setId(permDTO.getPermId());
        permDO.setPermName(permDTO.getPermName());
        permDO.setPermDesc(permDTO.getPermDesc());
        permDO.setPermType(permDTO.getPermType());
        permDO.setPermCode(permDTO.getPermCode());
        permDO.setModule(permDTO.getModule());
        permDO.setCreateTime(new Date());
        permDO.setUpdateTime(new Date());
        int result = permMapper.insert(permDO);
        return result > 0;
    }

    @Override
    public Boolean deletePerm(PermQuery permQuery) {
        SysPerm permDO = permMapper.selectById(permQuery.getPermId());
        if (permDO == null) {
            //资源不存在
            throw new BusinessException(RESOURCE_NOT_FOUND);
        }
        int result = permMapper.deleteById(permQuery.getPermId());
        return result > 0;
    }

    @Override
    public Boolean updatePerm(PermDTO permDTO) {
        SysPerm permDO = permMapper.selectById(permDTO.getPermId());
        if (permDO == null) {
            //资源不存在
            throw new BusinessException(RESOURCE_NOT_FOUND);
        }
        permDO.setPermName(permDTO.getPermName());
        permDO.setPermDesc(permDTO.getPermDesc());
        permDO.setPermType(permDTO.getPermType());
        permDO.setPermCode(permDTO.getPermCode());
        permDO.setModule(permDTO.getModule());
        permDO.setUpdateTime(new Date());
        int result = permMapper.updateById(permDO);
        return result > 0;
    }

    @Override
    public SysPerm getPerm(String permId) {
        SysPerm permDO = permMapper.selectById(permId);
        return permDO;
    }

    @Override
    public Boolean addRole(RoleDTO roleDTO) {
        QueryWrapper<SysRole> queryWrapper = Wrappers.<SysRole>query().eq("role_name", roleDTO.getRoleName());
        if (roleMapper.selectOne(queryWrapper) != null) {
            //角色名称已存在
            throw new BusinessException(ROLE_NAME_EXIST);
        }
        SysRole roleDO = new SysRole();
        roleDO.setId(roleDTO.getRoleId());
        roleDO.setRoleName(roleDTO.getRoleName());
        roleDO.setRoleDesc(roleDTO.getRoleDesc());
        roleDO.setTenantId(roleDTO.getTenantId());
        roleDO.setCreateTime(new Date());
        roleDO.setUpdateTime(new Date());
        int result = roleMapper.insert(roleDO);
        return result > 0;
    }

    @Override
    public Boolean deleteRole(RoleDTO roleDTO) {
        SysRole roleDO = roleMapper.selectById(roleDTO.getRoleId());
        if (roleDO == null) {
            //角色不存在
            throw new BusinessException(ROLE_NOT_FOUND);
        }
        int result = roleMapper.deleteById(roleDTO.getRoleId());
        return result > 0;
    }

    @Override
    public Boolean updateRole(RoleDTO roleDTO) {
        SysRole roleDO = roleMapper.selectById(roleDTO.getRoleId());
        if (roleDO == null) {
            throw new BusinessException(ROLE_NOT_FOUND);
        }
        roleDO.setRoleName(roleDTO.getRoleName());
        roleDO.setRoleDesc(roleDTO.getRoleDesc());
        roleDO.setTenantId(roleDTO.getTenantId());
        roleDO.setUpdateTime(new Date());
        int result = roleMapper.updateById(roleDO);
        return result > 0;
    }

    @Override
    public SysRole getRole(String roleId) {
        SysRole roleDO = roleMapper.selectById(roleId);
        return roleDO;
    }

    @Override
    public Boolean addRolePerm(RolePermDTO rolePermDTO) {
        QueryWrapper<SysRolePerm> queryWrapper = Wrappers.<SysRolePerm>query().eq("role_id", rolePermDTO.getRoleId()).eq("perm_id", rolePermDTO.getPermId());
        if (rolePermMapper.selectOne(queryWrapper) != null) {
            //角色权限已存在
            throw new BusinessException(ROLE_PERMISSION_EXIST);
        }
        SysRolePerm rolePermDO = new SysRolePerm();
        rolePermDO.setId(rolePermDTO.getRolePermId());
        rolePermDO.setRoleId(rolePermDTO.getRoleId());
        rolePermDO.setPermId(rolePermDTO.getPermId());
        rolePermDO.setDataScope(rolePermDTO.getDataScope());
        int result = rolePermMapper.insert(rolePermDO);
        return result > 0;
    }

    @Override
    public Boolean deleteRolePerm(RolePermDTO rolePermDTO) {
        SysRolePerm rolePermDO = rolePermMapper.selectById(rolePermDTO.getRolePermId());
        if (rolePermDO == null) {
            //角色权限不存在
            throw new BusinessException(ROLE_PERMISSION_EXIST);
        }
        int result = rolePermMapper.deleteById(rolePermDTO.getRolePermId());
        return result > 0;
    }

    @Override
    public Boolean updateRolePerm(RolePermDTO rolePermDTO) {
        SysRolePerm rolePermDO = rolePermMapper.selectById(rolePermDTO.getRolePermId());
        if (rolePermDO == null) {
            throw new BusinessException(ROLE_PERMISSION_EXIST);
        }
        rolePermDO.setRoleId(rolePermDTO.getRoleId());
        rolePermDO.setPermId(rolePermDTO.getPermId());
        rolePermDO.setDataScope(rolePermDTO.getDataScope());
        int result = rolePermMapper.updateById(rolePermDO);
        return result > 0;
    }

    @Override
    public SysRolePerm getRolePerm(String rolePermId) {
        SysRolePerm rolePermDO = rolePermMapper.selectById(rolePermId);
        return rolePermDO;
    }
}
